package org.scalacn.easyscala.comet


import net.liftweb.http._
import net.liftweb.util._
import net.liftweb.common._
import net.liftweb.util.Helpers._
import scala.xml._
import js._
import JsCmds._
import JE._

import org.scalacn.easyscala.model._
import org.scalacn.easyscala.actor._

class MessageCometActor extends CometActor {
  
  private var messages: List[Message] = Message.findAll
  override def localSetup() {
    super.localSetup()
    MessageActor() ! MessageActor.UserListen(this)
  }
    
  override def localShutdown() {
    super.localShutdown()
    MessageActor() ! MessageActor.UnUserlisten(this)
  }
    
  def render = {
    val toDisplay = messages.last.context.toJsCmd
    val jsId = "message";
    JsFunc("updateScalaResultTable", JsObj(("text",toDisplay)), jsId).cmd 
  }
    
  override def lowPriority = {
    case MessageActor.MessageReceived(msg) => {
      messages = Message.findAll
      reRender(false)
     }
  }
  
}
